package com.lmscn.lms.api.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/**
 * 资讯表
 * 
 * @author zhang.xx
 * @date 2016年11月6日13:50:01
 */
public class Article extends Model<Article> {

	public static final Article dao = new Article();

	/**
	 * 分页获取资讯
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public Page<Article> getArticlePage(int pageNumber, int pageSize, Long categoryId) {
		String select = "select t.id,t.title,t.create_time,t.browse,t.img,t.source ";
		String sqlExceptSelect = null;
		if (categoryId == null) {
			sqlExceptSelect = " from t_article t WHERE t.type = '0' ORDER BY t.create_time DESC ";
			return dao.paginate(pageNumber, pageSize, select, sqlExceptSelect);
		} else {
			sqlExceptSelect = " from t_article t WHERE t.category_id = ? AND t.type = '0' ORDER BY t.create_time DESC ";
			return dao.paginate(pageNumber, pageSize, select, sqlExceptSelect, categoryId);
		}
	}

	/**
	 * 分页获取资讯，条件为多个分类id的字符串
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @param categoryIds
	 * @return
	 */
	public Page<Article> getArticlePageByIds(int pageNumber, int pageSize, String categoryIds) {
		String sql = " FROM t_article t WHERE t.category_id IN (" + categoryIds
				+ ") AND t.type = '0' AND t.is_support_mobile = 1 ORDER BY t.create_time DESC ";

		return dao.paginate(pageNumber, pageSize, "select t.id,t.title,t.create_time,t.browse,t.img,t.source ", sql);
	}

	/**
	 * 去掉type的分页
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @param categoryIds
	 * @return
	 */
	public Page<Article> getArticlePageByIdsWithoutType(int pageNumber, int pageSize, String categoryIds) {
		String sql = " FROM t_article t WHERE t.category_id IN (" + categoryIds
				+ ") AND t.is_support_mobile = 1 ORDER BY t.create_time DESC ";

		return dao.paginate(pageNumber, pageSize, "select t.id,t.title,t.create_time,t.browse,t.img,t.source ", sql);
	}

	/**
	 * 首页资讯与文章的四个小的列表的获取
	 * 
	 * @date 2016年11月13日11:25:14
	 * 
	 * @param articleType
	 *            资讯（1）、文章（2）
	 * @param tag
	 *            中央精神（1）、地方动态（2）、专家解读（3）、热点词条（4）
	 * @return
	 */
	public List<Article> getFourFirstPageArticles(int articleType, long categoryId, int limit) {
		// 添加置顶的排序，2017年2月16日13:14:45
		String sql = "SELECT t.id,t.article_type,t.category_id, t.title,t.img,t.create_time,t.source,t.browse"
				+ " FROM t_article t WHERE t.category_id = ? AND t.article_type = ? " + " ORDER BY t.is_top DESC,t.create_time DESC limit ? ";
		return dao.find(sql, categoryId, articleType, limit);
	}

	/**
	 * 四个分类的二级列表的分页获取
	 * 
	 * @date 2016年11月13日17:04:36
	 * @param pageNumber
	 * @param pageSize
	 * @param categoryId
	 * @return
	 */
	public Page<Article> getArticlesPage(int pageNumber, int pageSize, long articleType, long categoryId, String sql) {
		return dao.paginate(pageNumber, pageSize, "select t.id,t.title,t.create_time,t.browse,t.img,t.source ", sql, categoryId,
				articleType);
	}

	/**
	 * 获取相关资讯
	 * 
	 * @date 2016年11月14日14:42:37
	 * @param articleType
	 * @param categoryId
	 * @return
	 */
	public List<Article> getFourAboutArticles(String keyWord) {
		String sql = "SELECT t.id,t.article_type,t.category_id, t.title,t.img,t.create_time,t.source,t.browse"
				// + " FROM t_article t WHERE t.key_words like '%" + keyWord +
				// "%' " + "limit 4 ";
				+ " FROM t_article t,t_article_category c WHERE t.category_id = c.id AND c.column_id <> 0 AND t.key_words like '%"
				+ keyWord + "%' " + "limit 4 ";
		return dao.find(sql);
	}

	public Article getSimpleArticleById(long id) {
		String sql = "SELECT t.id,t.article_type,t.category_id, t.title,t.img,t.create_time,t.source,t.browse"
				+ " FROM t_article t WHERE t.id = ? ";
		return dao.findFirst(sql, id);
	}

	/**
	 * 除去自己，
	 * 
	 * @param keyWord
	 * @return
	 */
	public List<Article> getFourAboutArticlesWithoutOwn(String keyWord, Long articelId) {
		String sql = "SELECT t.id,t.article_type,t.category_id, t.title,t.img,t.create_time,t.source,t.browse"
				// + " FROM t_article t WHERE t.id != " + articelId +
				// " and t.key_words like '%" + keyWord + "%' " + "limit 4 ";
				+ " FROM t_article t,t_article_category c WHERE t.category_id = c.id AND c.column_id <> 0 AND t.id != "
				+ articelId + " and t.key_words like '%" + keyWord + "%' " + "limit 4 ";
		return dao.find(sql);
	}

	public Page<Article> getPageLikeQueryString(int pageNumber, int pageSize, String sql, Object[] paras) {
		return dao.paginate(pageNumber, pageSize, "select t.* ", sql, paras);
	}

}
